﻿using CardMonitor.Model;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;

namespace CardMonitor
{
    /// <summary>
    /// App.xaml 的交互逻辑
    /// </summary>
    public partial class App : Application
    {
        public static Config config = null;
        public static GlobalVariables globalVariables = null;
        private static object _lock = new object();
        private static System.Threading.Mutex IMutex;
        public static Config getConfig()
        {
            if (config == null)
            {
                lock (_lock)
                {
                    if (config == null)
                    {
                        config = new Config();
                    }
                }
            }
            return config;
        }
        public static GlobalVariables getGlobalVariables()
        {
            if (globalVariables == null)
            {
                lock (_lock)
                {
                    if (globalVariables == null)
                    {
                        globalVariables = new GlobalVariables();
                    }
                }
            }
            return globalVariables;
        }
        protected override void OnStartup(StartupEventArgs e)
        {
            string MyAppID = "OnlyRun_App";//运行进程互斥标识名称 可自行修改
            IMutex = new System.Threading.Mutex(true, MyAppID);
            if (IMutex.WaitOne(0, false))
            {

                base.OnStartup(e);

                App.getConfig().DeviceCode = ConfigurationManager.AppSettings["DeviceCode"];

                App.getConfig().HttpServerIp = ConfigurationManager.AppSettings["HttpServerIp"];
                App.getConfig().HttpServerPort = ConfigurationManager.AppSettings["HttpServerPort"];

                App.getConfig().IC_Card_PortName = ConfigurationManager.AppSettings["IC_Card_PortName"];
                App.getConfig().IO_Com_PortName = ConfigurationManager.AppSettings["IO_Com_PortName"];
                App.getConfig().IO_Com_Port = ConfigurationManager.AppSettings["IO_Com_Port"];

                App.getConfig().IDCardDevice = ConfigurationManager.AppSettings["IDCardDevice"];
                App.getConfig().driverMacCard = ConfigurationManager.AppSettings["driverMacCard"];
                App.getConfig().IDCardCode = ConfigurationManager.AppSettings["IDCardCode"];

                App.getConfig().Card_Device_PortName = ConfigurationManager.AppSettings["Card_Device_PortName"];

                App.getConfig().SettingPassword = ConfigurationManager.AppSettings["SettingPassword"];

                App.getConfig().AutoSettleAccounts = ConfigurationManager.AppSettings["AutoSettleAccounts"];
                App.getConfig().WorkMode = ConfigurationManager.AppSettings["WorkMode"];

                App.getConfig().IfPrint = ConfigurationManager.AppSettings["IfPrint"];
                App.getConfig().CardWindowCloseTime = ConfigurationManager.AppSettings["CardWindowCloseTime"];
                App.getConfig().IfStatement = ConfigurationManager.AppSettings["IfStatement"];
                App.getConfig().density = ConfigurationManager.AppSettings["density"];
                App.getConfig().statementMessage= ConfigurationManager.AppSettings["statementMessage"]; 
                App.getConfig().printDuration= ConfigurationManager.AppSettings["PrintDuration"]; 
                App.getConfig().printerName1= ConfigurationManager.AppSettings["PrinterName1"];
                App.getConfig().printerName2 = ConfigurationManager.AppSettings["PrinterName2"];
                App.getConfig().MaxWeigh = ConfigurationManager.AppSettings["MaxWeigh"];
                App.getConfig().PhotoHeight = ConfigurationManager.AppSettings["PhotoHeight"];
                App.getConfig().PhotoWidth = ConfigurationManager.AppSettings["PhotoWidth"];
                App.getConfig().ClearMultiples = ConfigurationManager.AppSettings["ClearMultiples"];
                App.getConfig().PhotoQuality = ConfigurationManager.AppSettings["PhotoQuality"];

                //App.getConfig().tipsPrint= ConfigurationManager.AppSettings["tipsPrint"];
            }
            else
            {
                MessageBox.Show("程序已经在运行！", "提示", MessageBoxButton.OK, MessageBoxImage.Asterisk);
                this.Shutdown();
            }
        }
    }
}